package com.zhudi.develop_demo_as.db;

import android.content.Context;

import com.zhudi.bean.DateTraffic;
import com.zhudi.dao.DaoSession;
import com.zhudi.dao.DateTrafficDao;
import com.zhudi.develop.util.ZhudiDateUtil;
import com.zhudi.develop_demo_as.MainApplication;

import java.util.List;

/**数据库操作工具类 */
public class FlowUtil {
    private static DaoSession mDaoSession;
    private static FlowUtil instance;
    private static DateTrafficDao trafficDao;


    public static FlowUtil getInstance(Context context) {

        if (instance == null) {

            synchronized (FlowUtil.class) {
                if (instance == null) {
                    instance = new FlowUtil();
                }
            }

            mDaoSession = MainApplication.getDaoSession(context);
            trafficDao = mDaoSession.getDateTrafficDao();
        }
        return instance;
    }

    /**删除全部 */
    public void clearAll(){
        trafficDao.deleteAll();
    }

    /**获取全部数据 */
    public List<DateTraffic> getAllFlow(){
       return trafficDao.loadAll();
    }

    /**获取某个类型的流量 0表示移动数据 1表示wifi数据 */
    public List<DateTraffic> getTypeFlow(int type){
        long today = ZhudiDateUtil.getToday();
        return trafficDao.queryBuilder()
                .where(DateTrafficDao.Properties.Date.eq(today), DateTrafficDao.Properties.Type.eq(type))
                .list();
    }
}
